from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse


def index(request):
    return render_to_response("myauth/login.html", {"user": request.user})


def mylogin(request):
    username = request.POST['username']
    pwd = request.POST['password']
    user = authenticate(username=username, password=pwd)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect(reverse('todo:index'))
        else:
            return HttpResponse(False)
    else:
        return HttpResponse(False)


def mylogout(request):
    logout(request)
    return HttpResponseRedirect(reverse('myauth:index'))
